package com.zbkj.crmeb.chant.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.CommonPage;
import com.common.PageParamRequest;
import com.constants.WeChatConstants;
import com.crmeb.core.common.CommonResult;
import com.crmeb.core.exception.CrmebException;
import com.github.pagehelper.PageInfo;
import com.zbkj.crmeb.chant.dao.ChannelLiveDao;
import com.zbkj.crmeb.chant.model.ChannelLive;
import com.zbkj.crmeb.chant.request.*;
import com.zbkj.crmeb.chant.service.ChannelLiveService;
import com.zbkj.crmeb.chant.vo.AssistantListVo;
import com.zbkj.crmeb.chant.vo.AssistantVo;
import com.zbkj.crmeb.chant.vo.RoomGoodsInfoVo;
import com.zbkj.crmeb.merchant.api.request.*;
import com.zbkj.crmeb.merchant.api.response.BroadcastRoleInfoResponse;
import com.zbkj.crmeb.merchant.api.response.BroadcastRoleResponse;
import com.zbkj.crmeb.merchant.api.response.RoomInfo;
import com.zbkj.crmeb.store.model.StoreLiveAssistant;
import com.zbkj.crmeb.store.model.StoreProductLive;
import com.zbkj.crmeb.store.service.StoreLiveAssistantService;
import com.zbkj.crmeb.store.service.StoreProductLiveService;
import com.zbkj.crmeb.wechat.service.WeChatService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 渠道商户直播表 服务实现类
 * </p>
 *
 * @author wuzp
 * @since 2022-07-11
 */
@Service
@Slf4j
public class ChannelLiveServiceImpl extends ServiceImpl<ChannelLiveDao, ChannelLive> implements ChannelLiveService {
    @Autowired
    private WeChatService weChatService;
    @Autowired
    private StoreProductLiveService storeProductLiveService;
    @Autowired
    private StoreLiveAssistantService storeLiveAssistantService;
    
    @Value("${server.wechatProgramToken}")
    private Boolean programTokenFlag;
    @Value("${live.merIds}")
    private List<Integer> liveMerIds;

    @Override
    public PageInfo<BroadcastRoleInfoResponse> getBroadcastRoleList(BroadcastRoleListRequest request, Integer merId) {
        if(request.getLimit() > 30 ){
            throw new CrmebException("该列表分页每页最大30");
        }
        //参数处理
        String param= "";
        if(request.getRole() != null){
            param += "&role="+request.getRole();
        }
        if(StringUtils.isNotBlank(request.getKeyword())){
            param += "&keyword="+request.getKeyword();
        }
        int offset = request.getLimit() * ( request.getPage() - 1);
        param += "&offset="+offset+"&limit="+request.getLimit();
        JSONObject jsonObject = weChatService.getByUri(WeChatConstants.WE_CHAT_BROADCAST_ROLE, param, merId);
        BroadcastRoleResponse response = JSONObject.toJavaObject(jsonObject, BroadcastRoleResponse.class);
        PageInfo<BroadcastRoleInfoResponse> pageInfo = new PageInfo<>();
        pageInfo.setList(response.getList());
        pageInfo.setPageNum(request.getPage());
        pageInfo.setPageSize(request.getLimit());
        Integer total = (Integer) jsonObject.get("total");
        pageInfo.setTotal(total);
        pageInfo.setPages((total + request.getLimit() - 1) / request.getLimit());
        return pageInfo;
    }

    @Override
    public CommonResult<Object> addBroadcastRole(BroadcastRoleRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("role", request.getRole());
        map.put("username", request.getUsername());
        JSONObject result = weChatService.postByUri(WeChatConstants.WE_CHAT_ADD_BROADCAST_ROLE, map, merId);
        if(result.containsKey("errcode")){
            if(result.getString("errcode").equals("0")){
                return CommonResult.success();
            }
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> deleteBroadcastRole(BroadcastRoleRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("role", request.getRole());
        map.put("username", request.getUsername());
        JSONObject result = weChatService.postByUri(WeChatConstants.WE_CHAT_DELETE_BROADCAST_ROLE, map, merId);
        if(result.containsKey("errcode")){
            if(result.getString("errcode").equals("0")){
                return CommonResult.success();
            }
            if(result.getString("errcode").equals("400005")){
                throw new CrmebException("主播角色删除失败，该主播存在未开播的直播间");
            }
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> addBroadcastGoods(BroadcastGoods request, Integer merId) {
        try {
            if(request.getName().getBytes("GBK").length > 28){
                throw new CrmebException("商品名称，最长14个汉字，1个汉字相当于2个字符");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if(request.getPriceType() != 1 && request.getPrice2() ==null){
            throw new CrmebException("价格不能为空");
        }
        if(request.getPriceType() == 2 && request.getPrice().compareTo(request.getPrice2()) > -1){
            throw new CrmebException("价格区间右边界必须大于左边界");
        }
        request.setUrl("pages/goods_details/index.html?id="+request.getGoodsId());
        GoodsInfo info = new GoodsInfo();
        BeanUtils.copyProperties(request,info);
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsInfo",info);
        JSONObject result = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_ADD,map,merId);
        //保存商品id ,审核单id
        if(result.containsKey("errcode") && result.getString("errcode").equals("0")){
            StoreProductLive storeProductLive = new StoreProductLive();
            storeProductLive.setProductId(request.getGoodsId());
            storeProductLive.setMerId(merId);
            storeProductLive.setGoodsId(Integer.parseInt(result.get("goodsId").toString()));
            storeProductLive.setAuditId(Integer.parseInt(result.get("auditId").toString()));
            storeProductLive.setPriceType(request.getPriceType());
            storeProductLive.setPrice(request.getPrice());
            storeProductLive.setPrice2(request.getPrice2());
            storeProductLive.setName(request.getName());
            storeProductLive.setUrl(request.getUrl());
            storeProductLive.setCoverImgUrl(request.getCoverImgUrl());
            storeProductLiveService.save(storeProductLive);
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public PageInfo<BroadcastGoods> getBroadcastGoods(Integer status, PageParamRequest pageParamRequest, Integer merId) {
        if(pageParamRequest.getLimit() > 100){
            throw new CrmebException("该列表分页每页最大100");
        }
        //参数处理
        String param= "&status="+status;
        int offset = pageParamRequest.getLimit() * ( pageParamRequest.getPage() - 1);
        param += "&offset="+offset+"&limit="+pageParamRequest.getLimit();
        JSONObject jsonObject = weChatService.getByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_LIST,param, merId);
        PageInfo<BroadcastGoods> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(pageParamRequest.getPage());
        pageInfo.setPageSize(pageParamRequest.getLimit());
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            JSONArray goods = jsonObject.getJSONArray("goods");
            List<BroadcastGoods> goodsInfos = goods.toJavaList(BroadcastGoods.class);
            pageInfo.setList(goodsInfos);
            Integer total = (Integer) jsonObject.get("total");
            pageInfo.setTotal(total);
            pageInfo.setPages((total + pageParamRequest.getLimit() - 1) / pageParamRequest.getLimit());
        }
        return pageInfo;
    }

    @Override
    public CommonResult<Object> updateBroadcastGoods(BroadcastGoods request, Integer merId) {
        if(request.getPriceType() != 1 && request.getPrice2() ==null){
            throw new CrmebException("价格不能为空");
        }
        if(request.getPriceType() == 2 && request.getPrice().compareTo(request.getPrice2()) > -1){
            throw new CrmebException("价格区间右边界必须大于左边界");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsInfo", request);
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_UPDATE, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> resetBroadcastGoods(ResetBroadcastGoodsRequest request, Integer merId) {
        StoreProductLive productLive = storeProductLiveService.getOne(Wrappers.<StoreProductLive>lambdaQuery().eq(StoreProductLive::getMerId, merId).eq(StoreProductLive::getGoodsId, request.getGoodsId()));
        if(productLive == null){
            throw new CrmebException("该商品不存在");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("auditId", productLive.getAuditId());
        map.put("goodsId", request.getGoodsId());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_RESET, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> deleteBroadcastGoods(Integer goodsId, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsId", goodsId);
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_DELETE, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public PageInfo<RoomInfo> getLiveList(PageParamRequest pageParamRequest, Integer merId) {
    	// 测试环境不查询数据
    	if(!programTokenFlag || -1 == liveMerIds.indexOf(merId)) {
    		return new PageInfo<RoomInfo>();
    	}
    	
        int offset = pageParamRequest.getLimit() * ( pageParamRequest.getPage() - 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", offset);
        map.put("limit", pageParamRequest.getLimit());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_List, map, merId);
        PageInfo<RoomInfo> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(pageParamRequest.getPage());
        pageInfo.setPageSize(pageParamRequest.getLimit());
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            JSONArray jsonArray = jsonObject.getJSONArray("room_info");
            List<RoomInfo> roomInfos = jsonArray.toJavaList(RoomInfo.class);
            if(CollectionUtils.isNotEmpty(roomInfos)){
                List<Integer> roomIdList = roomInfos.stream().map(RoomInfo::getRoomId).collect(Collectors.toList());
                List<ChannelLive> channelLiveList = this.list(Wrappers.<ChannelLive>lambdaQuery().in(ChannelLive::getRoomId, roomIdList).eq(ChannelLive::getMerId, merId).select(ChannelLive::getRoomId, ChannelLive::getAnchorWechat, ChannelLive::getSubAnchorWechat));
                Map<Integer, ChannelLive> channelLiveMap = channelLiveList.stream().collect(Collectors.toMap(ChannelLive::getRoomId, e -> e, (v1, v2) -> v2));
                for (RoomInfo roomInfo : roomInfos) {
                    for (RoomGoodsInfoVo good : roomInfo.getGoods()) {
                        good.setPrice(good.getPrice().movePointLeft(2));
                        if(good.getPrice2() != null){
                            good.setPrice2(good.getPrice2().movePointLeft(2));
                        }
                    }
                    roomInfo.setStartTime(new Date(roomInfo.getStartTime().getTime()*1000));
                    roomInfo.setEndTime(new Date(roomInfo.getEndTime().getTime()*1000));
                    ChannelLive channelLive = channelLiveMap.get(roomInfo.getRoomId());
                    if(channelLive != null){
                        roomInfo.setAnchorWechat(channelLive.getAnchorWechat());
                        roomInfo.setSubAnchorWechat(channelLive.getSubAnchorWechat());
                    }
                }
            }
            pageInfo.setList(roomInfos);
            Integer total = (Integer) jsonObject.get("total");
            pageInfo.setTotal(total);
            pageInfo.setPages((total + pageParamRequest.getLimit() - 1) / pageParamRequest.getLimit());
        }
        return pageInfo;
    }

    @Override
    public CommonResult<Object> addLive(AddLiveRequest request, Integer merId) {
        try {
            if(request.getName().getBytes("GBK").length > 34 || request.getName().getBytes("GBK").length < 6){
                throw new CrmebException("直播间名字，最短3个汉字，最长17个汉字，1个汉字相当于2个字符");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HashMap<String, Object> map = new HashMap<>();
        try {
            Field[] declaredFields = request.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(request));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_CREATE, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            ChannelLive channelLive = new ChannelLive();
            BeanUtils.copyProperties(request,channelLive,"startTime","endTime");
            channelLive.setTheme(request.getName());
            channelLive.setMerId(merId);
            channelLive.setStartTime(new Date(request.getStartTime() * 1000));
            channelLive.setEndTime(new Date(request.getEndTime() * 1000));
            channelLive.setRoomId(Integer.parseInt(jsonObject.get("roomId").toString()));
            this.save(channelLive);
        }
        return CommonResult.success(jsonObject);
    }

    @Override
    public CommonResult<Object> getLiveInfo(Integer roomId, PageParamRequest pageParamRequest, Integer merId) {
        int offset = pageParamRequest.getLimit() * ( pageParamRequest.getPage() - 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("action", "get_replay");
        map.put("room_id", roomId);
        map.put("start", offset);
        map.put("limit", pageParamRequest.getLimit());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GETLIVE_INFO, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){

        }
        return null;
    }

    @Override
    public CommonResult<Object> editRoom(EditRoomRequest request, Integer merId) {
        try {
            if(request.getName().getBytes("GBK").length > 34 || request.getName().getBytes("GBK").length < 6){
                throw new CrmebException("直播间名字，最短3个汉字，最长17个汉字，1个汉字相当于2个字符");
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        HashMap<String, Object> map = new HashMap<>();
        try {
            Field[] declaredFields = request.getClass().getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                map.put(field.getName(), field.get(request));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_EDIT_ROOM, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            ChannelLive channelLive = Optional.ofNullable(this.getOne(Wrappers.<ChannelLive>lambdaQuery().eq(ChannelLive::getRoomId, request.getId()))).orElse(new ChannelLive());
            BeanUtils.copyProperties(request,channelLive,"id","startTime","endTime");
            channelLive.setTheme(request.getName());
            channelLive.setMerId(merId);
            channelLive.setStartTime(new Date(request.getStartTime() * 1000));
            channelLive.setEndTime(new Date(request.getEndTime() * 1000));
            channelLive.setRoomId(request.getId());
            this.saveOrUpdate(channelLive);
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> deleteRoom(Integer roomId, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", roomId);
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_DELETE_ROOM, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            this.remove(Wrappers.<ChannelLive>lambdaQuery().eq(ChannelLive::getRoomId,roomId));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> getSharedCode(Integer roomId, String params, Integer merId) {
        //参数处理
        if(roomId == null){
            throw new CrmebException("请选择直播间");
        }
        String param = "&roomId="+roomId;
        if(StringUtils.isNotBlank(params)){
            param += "&params="+params;
        }
        JSONObject jsonObject = weChatService.getByUri(WeChatConstants.WE_CHAT_BROADCAST_GET_SHARED_CODE,param, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success(jsonObject);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> getAssistantList(Integer roomId, Integer merId) {
        String param = "&roomId="+roomId;
        JSONObject jsonObject = weChatService.getByUri(WeChatConstants.WE_CHAT_BROADCAST_GET_ASSISTANT_LIST,param, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            AssistantListVo assistantListVo = jsonObject.toJavaObject(AssistantListVo.class);
            if(CollectionUtils.isNotEmpty(assistantListVo.getList())){
                List<String> usernameList = assistantListVo.getList().stream().map(AssistantListVo.WechatUserInfo::getAlias).collect(Collectors.toList());
                List<StoreLiveAssistant> liveAssistantList = storeLiveAssistantService.list(Wrappers.<StoreLiveAssistant>lambdaQuery().eq(StoreLiveAssistant::getRoomId, roomId).in(StoreLiveAssistant::getUsername, usernameList));
                Map<String, String> map = liveAssistantList.stream().collect(Collectors.toMap(StoreLiveAssistant::getUsername, StoreLiveAssistant::getNickname));
                for (AssistantListVo.WechatUserInfo wechatUserInfo : assistantListVo.getList()) {
                    String nickname = map.get(wechatUserInfo.getAlias());
                    if(StringUtils.isNotBlank(nickname)){
                        wechatUserInfo.setNickname(nickname);
                    }
                }
            }
            return CommonResult.success(JSONObject.toJSON(assistantListVo));
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> addAssistant(AssistantRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        AssistantVo vo = new AssistantVo();
        BeanUtils.copyProperties(request,vo);
        map.put("users",new Object[]{vo});
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_ADD_ASSISTANT, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            StoreLiveAssistant storeLiveAssistant = new StoreLiveAssistant();
            BeanUtils.copyProperties(request,storeLiveAssistant);
            storeLiveAssistantService.save(storeLiveAssistant);
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> modifyAssistant(AssistantRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("username", request.getUsername());
        map.put("nickname", request.getNickname());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_MODIFY_ASSISTANT, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            StoreLiveAssistant storeLiveAssistant = Optional.ofNullable(storeLiveAssistantService.getOne(Wrappers.<StoreLiveAssistant>lambdaQuery()
                    .eq(StoreLiveAssistant::getRoomId, request.getRoomId())
                    .eq(StoreLiveAssistant::getUsername, request.getUsername()))).orElse(new StoreLiveAssistant());
            BeanUtils.copyProperties(request,storeLiveAssistant);
            storeLiveAssistantService.saveOrUpdate(storeLiveAssistant);
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> removeAssistant(RemoveAssistantRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("username", request.getUsername());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_REMOVE_ASSISTANT, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            storeLiveAssistantService.remove(Wrappers.<StoreLiveAssistant>lambdaQuery()
                    .eq(StoreLiveAssistant::getRoomId,request.getRoomId()).eq(StoreLiveAssistant::getUsername,request.getUsername()));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> updateFeedPublic(FeedPublicRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("isFeedsPublic", request.getIsOpen());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_UPDATE_FEED_PUBLIC, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            this.update(Wrappers.<ChannelLive>lambdaUpdate().set(ChannelLive::getIsFeedsPublic,request.getIsOpen()).eq(ChannelLive::getRoomId,request.getRoomId()));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> updateReplay(ReplayRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("closeReplay", request.getIsClose());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_UPDATE_REPLAY, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            this.update(Wrappers.<ChannelLive>lambdaUpdate().set(ChannelLive::getCloseReplay,request.getIsClose()).eq(ChannelLive::getRoomId,request.getRoomId()));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> updateKf(ReplayRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("closeKf", request.getIsClose());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_UPDATE_KF, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            this.update(Wrappers.<ChannelLive>lambdaUpdate().set(ChannelLive::getCloseKf,request.getIsClose()).eq(ChannelLive::getRoomId,request.getRoomId()));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> updateComment(FeedPublicRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("banComment", request.getIsOpen());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_UPDATE_COMMENT, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            this.update(Wrappers.<ChannelLive>lambdaUpdate().set(ChannelLive::getCloseComment,request.getIsOpen()).eq(ChannelLive::getRoomId,request.getRoomId()));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> onSaleGoods(OnSaleGoodsRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("goodsId", request.getGoodsId());
        map.put("onSale", request.getOnSale());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_ON_SALE, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> deleteGoodsInRoom(RoomGoodsRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("goodsId", request.getGoodsId());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_DELETEINROOM, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> pushGoods(RoomGoodsRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("goodsId", request.getGoodsId());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_PUSH, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> sortGoods(SortGoodsRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("goods", request.getGoods());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_SORT, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> addRoomGoods(AddRoomGoodsRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("ids", request.getIds());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_ROOM_ADDGOODS, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> getSubanchor(Integer roomId, Integer merId) {
        String param = "&roomId="+roomId;
        JSONObject jsonObject = weChatService.getByUri(WeChatConstants.WE_CHAT_BROADCAST_ROOM_GET_SUBANCHOR,param, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success(jsonObject);
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> addSubanchor(RoomSubanchorRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("username", request.getUsername());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_ROOM_ADD_SUBANCHOR, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> modifySubanchor(RoomSubanchorRequest request, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", request.getRoomId());
        map.put("username", request.getUsername());
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_ROOM_MODIFY_SUBANCHOR, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> deleteSubanchor(Integer roomId, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("roomId", roomId);
        JSONObject jsonObject = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_ROOM_DELETE_SUBANCHOR, map, merId);
        if(jsonObject.containsKey("errcode") && jsonObject.getString("errcode").equals("0")){
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @Override
    public CommonResult<Object> auditBroadcastGoods(Integer goodsId, Integer merId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("goodsId",goodsId);
        JSONObject result = weChatService.postByUri(WeChatConstants.WE_CHAT_BROADCAST_GOODS_AUDIT,map,merId);
        //保存商品id ,审核单id
        if(result.containsKey("errcode") && result.getString("errcode").equals("0")){
            storeProductLiveService.update(Wrappers.<StoreProductLive>lambdaUpdate().set(StoreProductLive::getAuditId,Integer.parseInt(result.get("auditId").toString())).eq(StoreProductLive::getGoodsId,goodsId));
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

}
